/**
 * 
 */
package com.huarui.notice.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.PagingInfo;

import com.huarui.notice.INoticeService;
import com.huarui.notice.Notice;
import com.huarui.user.security.UserHolder;
import com.opensymphony.xwork2.Action;

/**
 * @author sunpeng
 * 
 */
public class NoticeAction extends BaseAction {
	private Logger logger = Logger.getLogger(NoticeAction.class);
	private static final long serialVersionUID = -879784368788447151L;
	private INoticeService noticeService;
	private int start = 0;
	private int limit = 20;
	private String type; // 消息分类查询{dealed:true};
	private String noticeIds;

	public String list() {
		List<Notice> notices = this.noticeService.getMyReceivingNotices();
		ServletActionContext.getContext().put("jsonResult", notices);
		return Action.SUCCESS;
	}

	public String deal() {
		boolean result = false;
		Integer[] ids = this.getNoticeIds();
		logger.info("处理的消息ID号：" + this.noticeIds);
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (ids != null && ids.length > 0) {
			result = this.noticeService.deal(ids, true,map);
		}
		map.put("success", result);
		ServletActionContext.getContext().put("jsonResult", map);
		return Action.SUCCESS;

	}

	/**
	 * 取消处理消息
	 * 
	 * @return
	 */
	public String revert() {
		boolean result = false;
		Integer[] ids = this.getNoticeIds();
		logger.info("处理的消息ID号：" + this.noticeIds);
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (ids != null && ids.length > 0) {
			result = this.noticeService.deal(ids, false,map);
		}
		map.put("success", result);
		ServletActionContext.getContext().put("jsonResult", map);
		return Action.SUCCESS;
	}

	public String remove() {
		boolean result = false;
		Integer[] ids = this.getNoticeIds();
		logger.info("删除的消息ID号：" + this.noticeIds);
		if (ids != null && ids.length > 0) {
			result = this.noticeService.remove(ids);
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("success", result);
		ServletActionContext.getContext().put("jsonResult", map);
		return Action.SUCCESS;
	}

	public String paging() {
		PagingInfo<Notice, Notice> paginInfo = new PagingInfo<Notice, Notice>();
		Notice condition = this.getType();
		paginInfo.setCondition(condition);
		paginInfo.setStart(this.start);
		paginInfo.setLimit(this.limit);
		this.noticeService.getPagingMyReceivingNotices(paginInfo);
		ServletActionContext.getContext().put("jsonResult", paginInfo);
		return Action.SUCCESS;
	}

	public String count() {

		int noticyId = 0;
		try {
			noticyId = Integer.parseInt(this.noticeIds);
		} catch (Exception e) {
		}
		logger.info("消息最大ID号：" + noticyId);
		int total = this.noticeService.getMyReceivingNoticeCount(noticyId);
		if (total > 0) {
			Notice totalNotice = new Notice();
			totalNotice.setId(0);
			totalNotice.setTitle("未读消息提醒");
			totalNotice.setContent("您还有" + total + "条未读消息！");
			totalNotice.setReceiverId(UserHolder.getCurrentSessionMessage().getUserId().intValue());
			List<Notice> notices = new ArrayList<Notice>();
			notices.add(totalNotice);
			ServletActionContext.getContext().put("jsonResult", notices);
		} else {
			ServletActionContext.getContext().put("jsonResult", null);
		}
		return Action.SUCCESS;
	}

	public String detail() {
		int noticyId = Integer.parseInt(this.noticeIds);
		Notice notice = this.noticeService.read(noticyId);
		ServletActionContext.getContext().put("jsonResult", notice);
		return Action.SUCCESS;
	}

	public void setNoticeService(INoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Notice getType() {
		logger.info("查询的类型信息:" + this.type);
		if (StringUtils.isEmpty(this.type)) {
			return new Notice();
		}
		JSONObject jobj = JSONObject.fromObject(this.type);
		Notice notice = (Notice) JSONObject.toBean(jobj, Notice.class);
		return notice;
	}

	public static void main(String[] a) {
		NoticeAction n = new NoticeAction();
		n.type = "{dealed:true}";
		Notice r = n.getType();
		System.out.println("===" + r.isDealed());
	}

	public void setNoticeIds(String noticeIds) {
		this.noticeIds = noticeIds;
	}

	public Integer[] getNoticeIds() {
		if (StringUtils.isEmpty(this.noticeIds)) {
			return null;
		}
		String[] notices = this.noticeIds.split(",");
		List<Integer> ids = new ArrayList<Integer>();
		for (String n : notices) {
			if (StringUtils.isNotEmpty(n)) {
				ids.add(Integer.parseInt(n));
			}
		}
		return ids.toArray(new Integer[ids.size()]);
	}
}
